from django.http import JsonResponse
import json
# 导入 Customer
from list.models import Event
from app.models import User, EyeDetect, FacePose,GazeTrack
from django.http import HttpResponse
from django.conf import settings

BASE_DIR = settings.BASE_DIR

def listevent(request):
    # GET请求 参数在url中，同过request 对象的 GET属性获取
    if request.method == 'POST':
        request.params = json.loads(request.body)
    # 从请求消息中 获取修改客户的信息
    # 找到该客户，并且进行查询操作
        eventstatus = request.params['state']
        eventname = request.params['name']
    try:
        # 根据 id 从数据库中找到相应的客户记录
        event = Event.objects.filter(state=eventstatus,name=eventname).values()

    except Event.DoesNotExist:
        return {
            'ret': 1,
            'msg': f'status 为`{eventstatus}`的事件不存在'
        }

    retlist = list(event)
    return JsonResponse({'ret': 0, 'retlist': retlist})

# Eye
def listdetail(request):
    # GET请求 参数在url中，同过request 对象的 GET属性获取
    if request.method == 'POST':
        request.params = json.loads(request.body)
    # 从请求消息中 获取要查询的信息
        eventstatus = request.params['state']
        eventname = request.params['name']
        userCreatetime = request.params['datetime']

    try:
        # 从EyeDetect表中找到相应的记录
        userEar = EyeDetect.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userEar', flat=True)
        userTime = EyeDetect.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userTime', flat=True)
        userStatus = EyeDetect.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userStatus', flat=True)

    except EyeDetect.DoesNotExist:
        return {
            'ret': 1,
            'msg': f'status 为`{eventstatus}`的事件不存在'
        }

    ResponseResult = {"ear": list(userEar), "time": list(userTime),
                      "status": list(userStatus), "createtime": userCreatetime,
                      "category": "eye_detect"}

    return HttpResponse(json.dumps(ResponseResult, ensure_ascii=False),
                        content_type="application/json,charset=utf-8")

# Face
def listdetail1(request):
    # GET请求 参数在url中，同过request 对象的 GET属性获取
    if request.method == 'POST':
        request.params = json.loads(request.body)
    # 从请求消息中 获取修改客户的信息
    # 找到该客户，并且进行查询操作
        eventstatus = request.params['state']
        eventname = request.params['name']
        userCreatetime = request.params['datetime']

    try:
        # 从数据库中找到相应的客户记录
        userPitch = FacePose.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userPitch', flat=True)

        userTime = FacePose.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userTime', flat=True)
        userStatus = FacePose.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userStatus', flat=True)

    except FacePose.DoesNotExist:
        return {
            'ret': 1,
            'msg': f'status 为`{eventstatus}`的事件不存在'
        }

    ResponseResult = {"pitch": list(userPitch), "time": list(userTime),
                      "status": list(userStatus), "createtime": userCreatetime,
                      "category": "face_pose_detect",}

    return HttpResponse(json.dumps(ResponseResult, ensure_ascii=False),
                        content_type="application/json,charset=utf-8")



# Gazetrack
def listdetail3(request):
    # GET请求 参数在url中，同过request 对象的 GET属性获取
    if request.method == 'POST':
        request.params = json.loads(request.body)
    # 从请求消息中 获取修改客户的信息
    # 找到该客户，并且进行查询操作
        eventstatus = request.params['state']
        eventname = request.params['name']
        userCreatetime = request.params['datetime']
    try:
        # 从数据库中找到相应的客户记录
        userGaze = GazeTrack.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userGaze', flat=True)

        userTime = GazeTrack.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userTime', flat=True)
        userStatus = GazeTrack.objects.filter(userDetectClass=eventstatus, userCreateTime=userCreatetime,
                                         userNickName=eventname).values_list('userStatus', flat=True)

    except GazeTrack.DoesNotExist:
        return {
            'ret': 1,
            'msg': f'status 为`{eventstatus}`的事件不存在'
        }

    ResponseResult = {"gaze": list(userGaze), "time": list(userTime),
                      "status": list(userStatus), "createtime": userCreatetime,
                      "category": "gaze_tracking"}

    return HttpResponse(json.dumps(ResponseResult, ensure_ascii=False),
                        content_type="application/json,charset=utf-8")